import pygame

class guiItem_c:
	class slider_c:
		def __init__(self, width, totalHeight, sliderPercent):
			self.loc=0

		def __del__(self):
			pass

		def getLoc(self):
			return loc

	def __init__(self, pos, size):
		self.size = size
		self.pos = pos
		self.surface = pygame.Surface(self.size).convert_alpha()
		self.font = pygame.font.Font(None, 24 )
		self.vScrollWidth = 20
		self.hScrollWidth = 20
		self.borderWidth = 2
		self.padding = 4
		self.lineHeight = 24
		#self.colorIndent = ((100,100,100),(150,150,150),(200,200,200))
		self.colorIndent = self.genColorSet((150,150,150), True)
		self.colorOutdent = self.genColorSet((190,190,190))

	#pass colors in in a ((),(),())
	def drawRect(self, rect, colorSet=None):
		borderWidth=1
		if colorSet==None:
			colorSet=((200,200,200),(150,150,150),(100,100,100))
		self.surface.fill(colorSet[1], rect)
		#bot
		self.surface.fill(colorSet[2], (rect[0], rect[1]+rect[3]-borderWidth, rect[2], borderWidth))
		#right
		self.surface.fill(colorSet[2], (rect[0]+rect[2]-borderWidth, rect[1], borderWidth, rect[3]))
		#left
		self.surface.fill(colorSet[0], (rect[0], rect[1], borderWidth, rect[3]))
		#top
		self.surface.fill(colorSet[0], (rect[0], rect[1], rect[2], borderWidth))

	def click(self, pos):
		pass

	def genColorSet(self, color, reverse = False):
		colDiff=50
		colorHigh=(color[0]+colDiff,color[1]+colDiff,color[2]+colDiff)
		colorLow=(color[0]-colDiff,color[1]-colDiff,color[2]-colDiff)

		i=0
		while i<3:
			if colorHigh[i]>255:
				colorHigh[i]=255
			i+=1

		i=0
		while i<3:
			if colorLow[i]<0:
				colorLow[i]=0
			i+=1
		
		if reverse:
			swapColor=colorLow
			colorLow=colorHigh
			colorHigh=swapColor
		
		return (colorHigh, color, colorLow)

class button_c(guiItem_c):
	def __init__(self, pos, size, title, funcToCall):
		guiItem_c.__init__(self, pos, size)
		self.title = title
		self.activateFunction = funcToCall

	def click(self, pos):
		self.activateFunction()

	def draw(self):
		self.drawRect((0, 0, self.size[0], self.size[1]), self.colorOutdent)
		return self.surface	

#this should just be a list object, with the fact that it's a network list handled somewhere else.
#also should take as many args as there are rows from the row input func
class networkGameList_c(guiItem_c):
	class networkGame_c:
		def __init__(self, name = "Default game", address = "127.0.0.1", ping = 0):
			self.name = name
			self.address = address
			self.ping = ping

		def __del__(self):
			pass

		def getInfo(self):
			return self.name + " - " + self.address + " - Ping:" + str(self.ping) + "ms"

	def __init__(self, pos, size):
		self.items = []
		guiItem_c.__init__(self, pos, size)
		self.selectedGame = 0
		self.vScrollWidth = 20
		self.hScrollWidth = 20
		
		self.listOffset = 0
		
		self.borderWidth = 2
		self.padding = 2
		self.slider = guiItem_c.slider_c(20, 80, 10)

	def __del__(self):
		del self.surface
		while len(self.items)>0:
			del self.items[0]

	def scrollTo(self, x, y):
		self.listOffset = (x,y)
		self.updateSlider()

	#slider should prolly be a new class
	def drawVSlider(self):
		self.drawRect((self.size[0] - self.vScrollWidth,  self.padding, self.vScrollWidth - self.padding, self.size[1] - self.padding*2), self.colorOutdent)
	
	#i don't know if i like this method, returning a surface to be rendered a second time seems very OO but kinda sloppy speed-wise
	#maybe i should create a max func that will chop shit off if it's outside the area required, ie the x+width, if it's bigger than the display area, is reduced to the display area's max
	#specialised blit function?
	def draw(self):
		self.drawRect((0, 0, self.size[0], self.size[1]),self.colorIndent)
		i=0
		while i < len(self.items):
			if i == self.selectedGame:
				self.surface.blit(self.font.render( self.items[i].getInfo(), 0, (100, 100, 255)), (self.padding, self.padding + i * self.lineHeight))
			else:
				self.surface.blit(self.font.render( self.items[i].getInfo(), 0, (255, 255, 255)), (self.padding, self.padding + i * self.lineHeight))
			i+=1
		self.drawVSlider()
		return self.surface
	
	#this should be just looking for servers
	def addItem(self, itemText):
		self.items += [self.networkGame_c(itemText)]

	def clearItems(self):
		self.items=[]

	def refresh(self):
		pass

	def click(self, pos):
		self.selectedGame = int((pos[1]-self.pos[1]) / self.lineHeight)